package com.thinkgem.jeesite.common.utils;

import org.joda.time.DateTime;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class HMZUploadUtil {

    public static String upload(String uploadDir, MultipartFile file, String openid) throws IOException {
        //上传文件名后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        //上传文件名
        String fileName = openid + new DateTime().getMillis() + suffix;
        //服务器保存的文件对象
        File serverFile = new File("src/main/webapp"+uploadDir, fileName);
        System.out.println(ResourceUtils.CLASSPATH_URL_PREFIX+uploadDir);
        if (!serverFile.exists() && !serverFile.isDirectory()) {
            serverFile.mkdir();
        }
        //将上传的文件写入到服务器端文件内
        file.transferTo(serverFile);
        return fileName;
    }

}
